<script type="text/javascript" src="<?=base_url()?>public/js/jquery.form.js"></script>
<script type="text/javascript">
    function paginator(page, count){
        var ul = $('.paginator');
        $('a[href="last"]', ul).attr('href', count);
        var container = $('div', ul);
        container.html('');
        for(var i=1; i <= count; i++){
            if(i == page)
                container.append('<li class="active"><a href="' + i + '">' + i + '</a></li>');
            else
                container.append('<li><a href="' + i + '">' + i + '</a></li>');
        }
    }
    
    function fillFilesTable(data){
        var container = $('.files-table tbody');
        container.html('');
        $(data).each(function(){
            var string = "<tr>";
            string += "<td>" + this.id + "</td>";
            string += "<td>" + this.name + "</td>";
            string += "<td>" + this.autors + "</td>";
            string += "<td>" + this.comment + "</td>";
            string += "<td>" + this.size + "</td>";
            string += "<td>" + this.time + "</td>";
            string += '<td style="white-space:nowrap"><a class="btn btn-info fleft" href="<?=site_url("programs_managment/program")?>/' + this.id + '">Náhľad</a>';
            <?php if($this->user->isLogged()){ ?>
                string += ' <a class="btn btn-success fleft" href="<?=site_url("programs_managment/edit_program")?>/' + this.id + '">Edituj</a>';
                string += ' <a class="btn btn-danger fleft" href="<?=site_url("programs_managment/delete_program")?>/' + this.id + '">Vymaž</a>';
               
            <?php } ?>
            string += "</td></tr>";
            container.append(string);
        });
    }
    
    $(document).ready(function(){
        paginator(<?=$page?>, <?=$pageCount?>);
        fillFilesTable(<?=$files?>);
        
        var ajaxOptions = {
            success: function(response, status, xhr, $form){
                var json = $.parseJSON(response);
                var data = json.data;
                fillFilesTable(data);
                paginator(json.page, json.pageCount);
            }
        };
    
        $('button[type=submit].btn-success').on('click', function(e) { 
            e.preventDefault();
            
            $(this).closest('form').find('input[name=page]').val(1);
            $('#filter').ajaxSubmit(ajaxOptions);
            
            return false; 
        }); 
        
        $('.paginator a').live('click', function(e){
            e.preventDefault();
            var page = $('#filter input[name="page"]');
            var maxPage = $('.paginator a').last().attr('href');
            var href = $(this).attr('href');
            var actPage = parseInt(page.val());
            if(href == "prev"){
                if(actPage > 1){
                   page.val(actPage - 1);
                   $('#filter').ajaxSubmit(ajaxOptions);
                }
            }else if(href == "next"){
                if(actPage < maxPage){
                   page.val(actPage + 1);
                   $('#filter').ajaxSubmit(ajaxOptions);
                }
            }else{
                page.val(href);
                $('#filter').ajaxSubmit(ajaxOptions);
            }
        });
    });
</script>
<div class="page-header">
<h1>Vyhľadávanie softvéru</h1>
</div>
<form id="filter" class="form-horizontal" action="<?=site_url("programs_managment/filter")?>" method="post">
    <input type="hidden" name="page" value="1"/>
   
    <div class="row-fluid">
        <div class="span6">
    <div class="control-group">
    <?php echo form_label('Typ práce : ',"cond[wt]" ,array('class' =>'control-label'  )); ?>    
    <select name="cond[wt]">
        <option value="0"></option>
        <?php foreach($work_type as $row): ?>
        <option value="<?=$row->id?>"><?=$row->name?></option>
        <?php endforeach; ?>
    </select>
    </div>

    <div class="control-group">
    <?php echo form_label('Jazyk softvéru : ',"cond[l]" ,array('class' =>'control-label'  )); ?>    
    <select name="cond[l]">
        <option value="0"></option>
        <?php foreach($language as $row): ?>
        <option value="<?=$row->id?>"><?=$row->name?></option>
        <?php endforeach; ?>
    </select>
    </div>
    <div class="control-group">
         <center><button type="submit" class="btn btn-success">Vyhľadávať</button></center>
     </div>
    </div>
    <div class="span6">
     <div class="control-group">
    <?php echo form_label('Operačný systém : ',"cond[po]" ,array('class' =>'control-label'  )); ?>    
    <select name="cond[po]">
        <option value="0"></option>
        <?php foreach($program_os as $row): ?>
        <option value="<?=$row->id?>"><?=$row->name?></option>
        <?php endforeach; ?>
    </select>
    </div>
     <div class="control-group">
    <?php echo form_label('Určené pre : ',"cond[sl]" ,array('class' =>'control-label'  )); ?>    
    <select name="cond[sl]">
        <option value="0"></option>
        <?php foreach($study_level as $row): ?>
        <option value="<?=$row->id?>"><?=$row->name?></option>
        <?php endforeach; ?>
    </select>
    </div>
     <div class="control-group">
    <?php echo form_label('Predmet : ',"cond[cs]" ,array('class' =>'control-label'  )); ?>    
    <select name="cond[cs]">
        <option value="0"></option>
        <?php foreach($classroom_subject as $row): ?>
        <option value="<?=$row->id?>"><?=$row->name?></option>
        <?php endforeach; ?>
    </select>
    </div>
    </div>
    </div>
<div style="clear:both;"></div>
</form>


<div class="page-header">
    <h3>Výsledky vyhľadávania</h3>
</div>
<table class="files-table table table-hover table-condensed">
    <thead>
        <tr>
            <th><code>Id</code></th>
            <th><code>Názov</code></th>
            <th><code>Autori</code></th>
            <th><code>Komentár</code></th>
            <th><code>Velkosť </code></th>
            <th><code>Dátum</code></th>
            <th></th>
        </tr>
    </thead>
    <tbody>
        
    </tbody>
</table>
<div class="pagination .pagination-centered">
    <ul class="paginator">
        <li><a href="1"><<</a></li>
        <li><a href="prev"><</a></li>
        <div class="pagii" style="float:left;"></div>
        <li><a href="next">></a></li>
        <li><a href="last">>></a></li>
    </ul>
</div>